﻿// Dib.cpp: implementation of the CDib class.
//
//////////////////////////////////////////////////////////////////////

#include "stdafx.h"
#include "Dib.h"

#ifdef _DEBUG
#undef THIS_FILE
static char THIS_FILE[]=__FILE__;
#define new DEBUG_NEW
#endif

#include <iostream>
__declspec(dllexport) IDib* CreateIDib()
{
	//std::cout << __FUNCTION__ << "  " << __FILE__<< std::endl;
	return new CDib;
}

//////////////////////////////////////////////////////////////////////
// Construction/Destruction
//////////////////////////////////////////////////////////////////////

CDib::CDib()
{
	m_pBitmap = NULL;
	m_pMemDC = NULL;
	m_pBitmapTmp = NULL;
	m_hDC = NULL;
	m_hFileMapping = NULL;

	m_bBeginPaint = false;
}

CDib::~CDib()
{
	Destroy();
}

//////////////////////////////////////////////////////////////////////////
//
//////////////////////////////////////////////////////////////////////////

BOOL CDib::DoCreate()
{
	DIB_BITMAPINFO	bm;
	SetBitmapInfo( bm );
	
	if( m_pBitmap != NULL )
	{
		delete m_pBitmap;
		m_pBitmap = NULL;
	}
	if( m_hFileMapping )
	{
		CloseHandle( m_hFileMapping );
		m_hFileMapping = NULL;
	}

	HANDLE hBitmap = CreateDIBSection ( NULL, (BITMAPINFO *)&bm, DIB_RGB_COLORS, &m_pBits, 
		NULL, NULL ); 
	if( !hBitmap )
	{
#ifdef _DEBUG
		int d = GetLastError();
#endif
		return FALSE;
	}
	/*
	{
		m_hFileMapping = CreateFileMapping(INVALID_HANDLE_VALUE, NULL, PAGE_READWRITE | SEC_COMMIT, 
				0, m_nBitCount/8 * m_Size.cx * m_Size.cy + 10000000, "DIB");
		if(m_hFileMapping == NULL)
			return FALSE;

		hBitmap = CreateDIBSection ( NULL, (BITMAPINFO *)&bm, DIB_RGB_COLORS, &m_pBits, 
			m_hFileMapping, 0 ); 
		if( !hBitmap )
			return FALSE;
	}*/

	m_pBitmap = new CBitmap;
	m_pBitmap->Attach( hBitmap );
	return TRUE;
}

// 开始画
CDC* CDib::BeginPaint(int iValue, BOOL bFillInit)
{
	if( m_bBeginPaint )
		EndPaint();

	m_hDC = GetDC( NULL );
	CDC dc;
	CDC* pDC = dc.FromHandle( m_hDC );

	if( bFillInit )
		FillContent( iValue );

	m_pMemDC = new CDC;
	m_pMemDC->CreateCompatibleDC( pDC );
	m_pBitmapTmp = (CBitmap *)m_pMemDC->SelectObject( m_pBitmap );

	m_bBeginPaint = true;
	return m_pMemDC;
}

// 结束画
void CDib::EndPaint()
{
	m_pMemDC->SelectObject( m_pBitmapTmp );
	delete m_pMemDC;
	m_pMemDC = NULL;
	DeleteObject( m_hDC );
	m_hDC = NULL;
	m_bBeginPaint = false;
}

void CDib::Destroy()
{
	if( m_bBeginPaint )
		EndPaint();
	CDibBase::Destroy();

	if( m_pBitmap )
	{
		delete m_pBitmap;
		m_pBitmap = NULL;
	}
	if( m_hFileMapping )
	{
		CloseHandle( m_hFileMapping );
		m_hFileMapping = NULL;
	}

	m_pBits = NULL;
	m_pMemDC = NULL;
	m_pBitmapTmp = NULL;
	m_hDC = NULL;
}

// 
void CDib::BitBlt( CDC* pDC, LPRECT lpRcDest, int xSrc, int ySrc, DWORD dwRop )
{
	CDC MemDC;
	MemDC.CreateCompatibleDC( pDC );

	CBitmap* pOldBmp = MemDC.SelectObject( m_pBitmap );

	pDC->BitBlt( lpRcDest->left, lpRcDest->top, lpRcDest->right - lpRcDest->left, 
		lpRcDest->bottom - lpRcDest->top, &MemDC, xSrc, ySrc, dwRop );
	MemDC.SelectObject( pOldBmp );
}

void CDib::StretchBlt( CDC* pDC, LPRECT lpRcDest, LPRECT lpRcSrc, DWORD dwRop )
{
	CDC MemDC;
	MemDC.CreateCompatibleDC( pDC );

	CBitmap* pOldBmp = MemDC.SelectObject( m_pBitmap );

	BOOL b = pDC->StretchBlt( lpRcDest->left, lpRcDest->top, lpRcDest->right - lpRcDest->left, 
		lpRcDest->bottom - lpRcDest->top, &MemDC, lpRcSrc->left, lpRcSrc->top, 
		lpRcSrc->right - lpRcSrc->left, lpRcSrc->bottom - lpRcSrc->top, dwRop );

	MemDC.SelectObject( pOldBmp );
}

// 
void CDib::TransparentBlt( CDC* pDC, LPRECT lpRcDest, int xSrc, int ySrc )
{
	CDC MemDC;
	MemDC.CreateCompatibleDC( pDC );

	CBitmap* pOldBmp = MemDC.SelectObject( m_pBitmap );

	::TransparentBlt(pDC->GetSafeHdc(), lpRcDest->left, lpRcDest->top, 
		lpRcDest->right - lpRcDest->left, lpRcDest->bottom - lpRcDest->top, 
		MemDC.GetSafeHdc(), xSrc, ySrc,
		lpRcDest->right - lpRcDest->left, lpRcDest->bottom - lpRcDest->top, 
		RGB(0, 0, 0) );
	MemDC.SelectObject( pOldBmp );
}

